import axios from '@/api/interceptor';
import qs from 'query-string';
import { AnnouncementQuery, AnnouncementRecord } from './types';

export const selectPageAnnouncement = async (params: AnnouncementQuery) => {
  const result = await axios({
    url: '/announcement/listPage',
    method: 'GET',
    params,
    paramsSerializer: (obj) => {
      return qs.stringify(obj);
    },
  });
  return result.data;
};

export const createAnnouncement = async (data: AnnouncementRecord) => {
  const result = await axios({
    url: '/announcement/createAnnouncement',
    method: 'POST',
    data,
  });
  return result.data;
};

export const removeAnnouncement = async (ids: number[]) => {
  const result = await axios({
    url: '/announcement/removeAnnouncement',
    method: 'DELETE',
    data: ids,
  });
  return result.data;
};

export const getAnnouncementDetail = async (id: number) => {
  const result = await axios({
    url: '/announcement/getAnnouncementDetail',
    method: 'GET',
    params: { id },
  });
  return result.data;
};

export const editAnnouncement = async (data: AnnouncementRecord) => {
  const result = await axios({
    url: '/announcement/editAnnouncement',
    method: 'PUT',
    data,
  });
  return result.data;
};

export const editAnnouncementPinned = async (status: number, id: number) => {
  const result = await axios({
    url: '/announcement/pinned',
    method: 'PUT',
    params: { status, id },
  });
  return result.data;
};
